<template>
  <div class="layui-layout layui-layout-admin">
    <div class="adminnav1">
      <Pagetop></Pagetop>
    </div>
    <div class="adminnav2">
      <Secondnav urlname="news"></Secondnav>
    </div>

    <!-- 主体内容 -->
    <div class="layui-body kedou-admin" id="LAY_app_body app">
      <div class="layadmin-tabsbody-item layui-show">
        <div class="pagelist">
          <div class="layui-card-body">
            <fieldset class="layui-elem-field layui-field-title">
              <legend>添加营销模版</legend>
            </fieldset>
          </div>
          <div class="pagetop">
            <Search :seadate="searchArr"></Search>
          </div>
            <div class="layui-card-body">
            <div class="buttop">
              <button  class="layui-btn" @click="newshyadd">会员</button>
              <button  class="layui-btn layui-btn-normal" @click="newshdadd">活动</button>
              <!-- <button  class="layui-btn layui-btn-warm" @click="newscjadd">抽奖</button> -->
               <button  class="layui-btn layui-btn-danger" @click="newsyhjadd">优惠券</button>
            </div>
            <Tables
              :fdate="pdate"
              :result="res"
              height="430"
              :totalCount="totalRows"
              :pageNumber="curindex"
              :fbtn="btn"
            ></Tables>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Pagetop from "../common/pagetop.vue";
import Secondnav from "../common/secondnav.vue";
import Tables from "../common/tables.vue";
import Search from "../common/search.vue";
export default {
  name: "meadd",
  components: { Pagetop, Secondnav, Tables, Search },
  data() {
    return {
        pdate: [], //表头
      res: "", //内容
      totalRows: "", //总数量
      curindex: 1, //页数
      btn: [], //按钮
      searchArr: [], //搜索
      isShow: false,
      seapram: "",
      id: "",
      type: localStorage.getItem("type"),

    };
  },
  mounted() {
      this.getdate();
      this.getsearch();
  },
  methods: {
    getdate:function(){
        var _this=this;
        _this.id = this.$route.query.id
    	var url="/advert/aapi/v1.9/news-target"
    	if(_this.seapram){
    		var dates={
                size:pageSize,
	    		page:this.curindex,
	        //    sort:'createAt,DESC',
	           'newsId.equals':_this.id
	    	}
    		var res=_this.seapram;
    		for(var i=0;i<res.length;i++){
    			dates[res[i].key+'.equals']=res[i].val;
    		}

    	}else{
	    	var dates={
               size:pageSize,
	    	   page:this.curindex,
	        //    sort:'createAt,DESC',
	           'newsId.equals':_this.id
		    }
      }
    	ajaxRequest(url,'GET','json',dates,function(ret,err){
	  		var arr=[];
		  	arr[0]=[
		       {field: 'id', title: 'ID'}
		      ,{field: 'name', title: '标题'}
		     ,{
            field: "logo",
            title: "资讯logo",
            templet:
              '<div><img src="{{d.logo}}" style="display: inline-block; height: 100%;"></div>',
          }
		      ,{field: 'targetType', title: '营销模版',templet:function(d){
                  if(d.targetType==1) return '会员'
                  else if(d.targetType==2) return '活动'
                  else if(d.targetType==3) return '抽奖'
                  else  return '优惠券'
              }},
		      {
            field: "right",
            title: "操作",
            width: "120",
            fixed: "right",
            templet: function(d) {
              var str =
                '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
                 var str1 =
                '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
              return str + str1;
            }
          },
		    ];
		  	_this.pdate=arr;
		  	_this.res=ret.data.list;
		  	_this.totalRows=ret.data.totalRows;
		  	_this.curindex=ret.data.page+1;
	    })
    },
    getsearch:function(){
    	var arr=[
    		{field: 'title', title: '标题',type: 'input'}
    	];
    	this.searchArr=arr;
    },
    page:function(index,obj){
    	//alert(JSON,stringify(obj))
    	this.curindex=index;
    	this.getdate();

    },
    sousuo:function(pram){
    	//alert(JSON.stringify(pram))
    	this.seapram=pram;
    	this.res='';
    	this.totalRows='';
    	this.curindex='';
    	this.getdate();
    },
    newshyadd:function () {
        var _this = this;
      layer.open({
        type: 2,
        title: "选择会员模版",
        content: baseurl + '/newshyadd?id='+ this.id,
        area: _this.screens() < 2 ? ["90%", "90%"] : ["900px", "900px"],
      });
      },
      newshdadd:function () {
        var _this = this;
        // alert(JSON.stringify(_this.id))
      layer.open({
        type: 2,
        title: "选择活动模版",
        content: baseurl + '/newshdadd?id='+ this.id,
        area: _this.screens() < 2 ? ["90%", "90%"] : ["900px", "900px"],
      });
      },
      newscjadd:function () {
        var _this = this;
      layer.open({
        type: 2,
        title: "选择抽奖模版",
        content: baseurl + '/newscjadd?id='+ this.id,
        area: _this.screens() < 2 ? ["90%", "90%"] : ["900px", "900px"],
      });
      },
      newsyhjadd:function () {
        var _this = this;
      layer.open({
        type: 2,
        title: "选择优惠券模版",
        content: baseurl + '/newsyhjadd?id='+ this.id,
        area: _this.screens() < 2 ? ["90%", "90%"] : ["1200px", "900px"],
      });
      },
      screens: function () {
      return screen();
    },
    editInfo: function(data) {
      var _this = this;
      layer.open({
        type: 2,
        title: "编辑营销模版",
        content: baseurl + "/newsyxadd",
        area: _this.screens() < 2 ? ["70%", "50%"] : ["700px", "500px"],
        success: function(layero, index) {
          var iframeWin = window[layero.find("iframe")[0]["name"]];
          iframeWin.getdates(data);
        }
      });
    },
    delInfo: function (data) {
      var _this = this;
      if(_this.type == '1'){
      layer.confirm("是否确认删除？", function () {
        var url = "/advert/aapi/v1.9/news-target/" + data.id;
        ajaxRequest(url, "DELETE", "json", "", function (ret, err) {
          layer.msg("操作成功");
          setTimeout(function () {
            _this.getdate();
          }, 1000);
        });
      });
      }else{
        layer.msg("该角色无权删除营销模版")
      }
    },
  },
  updated: function() {
    var _this = this;
    layui.use("form", function() {
      var form = layui.form;
      form.render();
      form.render(null,'djlist');
    });
  }
};
</script>
<style scoped></style>
